package com.basker.pisces.core.query.criteria;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.basker.pisces.core.query.ICriteria;

/**
 * 复合条件，包含多个{@link Criterion}，前后有括号
 *
 * @author hangwen
 */
public class CompositeCriterion extends Criterion implements ICriteria {
    private List<Criterion> criteria = new ArrayList<>();

    private String desc;

    public CompositeCriterion() {
        this("Composite Criterion");
    }

    public CompositeCriterion(String desc) {
        this.desc = desc;
        this.setAndOr(Criterion.AND);
        this.setBeginBracket(true);
        this.setEndBracket(true);
    }

    public CompositeCriterion(String desc, String andOr) {
        this(desc);

        if (!StringUtils.isEmpty(andOr)) {
            this.setAndOr(andOr);
        }
    }

    @Override
    public void addCriteria(List<Criterion> criteria) {
        this.criteria.addAll(criteria);
    }

    @Override
    public void addCriterion(Criterion criterion) {
        this.criteria.add(criterion);
    }

    @Override
    public String getCondition() {
        return "";
    }

    @Override
    public List<Criterion> getCriteria() {
        return criteria;
    }

    public String getDesc() {
        return desc;
    }

    @Override
    public Object getValue() {
        return null;
    }

    @Override
    public boolean isEmpty() {
        return this.criteria.isEmpty();
    }

    @Override
    public boolean isParameterExpression() {
        return false;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        if (StringUtils.isEmpty(this.desc)) {
            return this.criteria.toString();
        }

        return desc;
    }
}
